﻿define(['dojo/_base/declare', 'geomap/plugins/layers/HoneycombLayer'], function (
    declare,
    BaseHoneycombLayer
) {
    var HoneycombLayer = declare([BaseHoneycombLayer], {
        setData: function (mapView, layer) {
            var options = layer.layer || {};
            var data = $.extend(true, [], options.data);
            this.data = data;
            this.lngFieldName = options.lngFieldName || 'x';
            this.latFieldName = options.latFieldName || 'y';
            this.countFieldName = options.countFieldName;
            this.devicePixelRatio = 1;
            this.options = options.options || {};
            this.options.minScale = options.minScale;
            this.options.maxScale = options.maxScale;
            options.gradient = this._setOpacity(this.options.gradient, this.options.opacity) || {
                0.25: 'rgba(0, 0, 255, 1)',
                0.55: 'rgba(0, 255, 0, 1)',
                0.85: 'rgba(255, 255, 0, 1)',
                1.0: 'rgba(255, 0, 0, 1)',
            };

            this.mapv = options.options.mapv;
            this.init();

            if (options.options.isShowLegend) {
                this.getLegend(options.options.legendOptions);
            }
            if (typeof options.callback == 'function') {
                options.callback(layer);
            }
        },
        _setOpacity: function (gradient, opacity) {
            if (!gradient) {
                return;
            }
            if (!opacity) {
                return;
            }
            for (var key in gradient) {
                if (gradient[key].includes('rgba')) {
                } else {
                    gradient[key] = gradient[key]
                        .replace('rgb', 'rgba')
                        .replace(')', `,${opacity})`);
                }
            }
            return gradient;
        },
    });
    return HoneycombLayer;
});
